﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using Login.BLL;

namespace Courses
{
    public partial class tb_banji : Form
    {
        public tb_banji()
        {
            InitializeComponent();
        }
        /// <summary>
        /// excel表格选取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void btn1_Click(object sender, EventArgs e)
        {
            //查找文件类的实例化
            OpenFileDialog fd = new OpenFileDialog();
            //判断是否选中文件
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string fileName = fd.FileName;
                //将选中文件放入框中
                bind(fileName);
            } 
        }
        //申明一个虚拟表的类
        DataSet ds;
        //申明一个操作Excel文档的类
        OleDbDataAdapter da;
        private void bind(string fileName)
        {
            //构造连接excel文档路径
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + fileName + ";" +
                "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
            //构造Excel文档造作方法
            da = new OleDbDataAdapter("SELECT *  FROM [Sheet1$]", strConn);
            ds = new DataSet();
            try
            {
                //将excel文档内容提取出来
                da.Fill(ds);
                this.biao.DataSource = ds.Tables[0];
            }
            catch (Exception err)
            {
                MessageBox.Show("操作失败！" + err.ToString());
            }
        }   
        #endregion



        /// <summary>
        /// 班级信息导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //判断表中是否含有数据
                if (JudgeData())
                {
                    //删除数据库原有班级信息
                    tb_banjiBLL.delete();
                    //循环表中所有行
                    for (int i = 0; i < biao.Rows.Count; i++)
                    {
                        //提取当前行列名为“班级名”的字符串
                        string className = biao.Rows[i].Cells["班级名"].Value.ToString();
                        //将该班级名称添加到数据库
                        tb_banjiBLL.ClassName(className);
                    }
                    MessageBox.Show("导入成功！");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        /// <summary>
        /// 实验信息导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void btn2_Click(object sender, EventArgs e)
        {
            try
            {
                //判断表中是否含有数据
                if (JudgeData())
                {
                    //删除数据库原有实验信息
                    tb_banjiBLL.deleteShiYan();
                    //循环表中所有行
                    for (int i = 0; i < biao.Rows.Count; i++)
                    {
                        //提取表中相关字符串
                        string jianCheng = biao.Rows[i].Cells["实验简称"].Value.ToString();
                        string mingChen = biao.Rows[i].Cells["实验名称"].Value.ToString();
                        string diDian = biao.Rows[i].Cells["实验地点"].Value.ToString();
                        string teachar = biao.Rows[i].Cells["实验教师"].Value.ToString();
                        //添加班级实验信息
                        tb_banjiBLL.shiYanMing(i + 1, jianCheng, mingChen, diDian, teachar);
                    }
                    MessageBox.Show("导入成功！");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        /// <summary>
        /// 判断表中是否含有数据
        /// </summary>
        #region
        private bool JudgeData()
        {
            //判断表中是否含有行
            if (biao.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                MessageBox.Show("没有数据或没有相关列名！");
                return false;
            }
        }
        #endregion
    }
}
